﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InstallByDrag
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            diaopen = new OpenFileDialog() { Filter = "MSI安装文件|*.msi" };

        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Note that you can have more than one file.
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files != null && files.Length > 0)
                {
                    var msiname = files[0].ToUpper();
                    if (msiname.EndsWith(".MSI") && File.Exists(msiname))
                    {
                        start(msiname);
                    }
                }
            }
        }

        OpenFileDialog diaopen;
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (diaopen.ShowDialog()==true)
            {
                start(diaopen.FileName);
            }
        }

        void start(string msiname)
        {
            Process np = new Process();
            np.StartInfo.FileName = $"{ Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)}/msiexec.exe";
            np.StartInfo.Arguments = $" /i \"{msiname}\"";
            np.Start();
        }
    }
}
